{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Original CapsNet Model Train\n",
    "\n",
    "In this notebook we provide a simple interface to train the original CapsNet model described in \"Dynamic routinig between capsules\". The model is copycat of the original Sara's repository (https://github.com/Sarasra/models/tree/master/research/capsules). <br>\n",
    "However, if you really reach 99.75, you've got to buy me a drink :)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:34:31.431990Z",
     "start_time": "2021-01-26T11:34:31.422498Z"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:34:33.092592Z",
     "start_time": "2021-01-26T11:34:31.773426Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from utils import Dataset, plotImages, plotWrongImages\n",
    "from models import CapsNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:34:33.173488Z",
     "start_time": "2021-01-26T11:34:33.126360Z"
    }
   },
   "outputs": [],
   "source": [
    "gpus = tf.config.experimental.list_physical_devices('GPU')\n",
    "tf.config.experimental.set_visible_devices(gpus[0], 'GPU')\n",
    "tf.config.experimental.set_memory_growth(gpus[0], True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:35:22.584111Z",
     "start_time": "2021-01-26T11:35:22.561220Z"
    }
   },
   "outputs": [],
   "source": [
    "# some parameters\n",
    "model_name = 'MNIST' # only MNIST is available\n",
    "n_routing = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.0 Import the Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:35:23.625015Z",
     "start_time": "2021-01-26T11:35:23.312138Z"
    }
   },
   "outputs": [],
   "source": [
    "dataset = Dataset(model_name, config_path='config.json') # only MNIST"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 Visualize imported dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:35:28.277156Z",
     "start_time": "2021-01-26T11:35:26.856157Z"
    }
   },
   "outputs": [],
   "source": [
    "n_images = 20 # number of images to be plotted\n",
    "plotImages(dataset.X_test[:n_images,...,0], dataset.y_test[:n_images], n_images, dataset.class_names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.0 Load the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:35:49.661386Z",
     "start_time": "2021-01-26T11:35:48.788560Z"
    }
   },
   "outputs": [],
   "source": [
    "model_train = CapsNet(model_name, mode='train', verbose=True, n_routing=n_routing)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.0 Train the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-26T11:41:04.159142Z",
     "start_time": "2021-01-26T11:40:57.403433Z"
    }
   },
   "outputs": [],
   "source": [
    "history = model_train.train(dataset, initial_epoch=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
